package com.ophiux.FeverManagement.api.portImpl;

import java.lang.reflect.Type;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.ophiux.FeverManagement.api.port.IPatientTreatmentPort;
import com.ophiux.FeverManagement.aplication.bean.addPatientTreatmentRequest;
import com.ophiux.FeverManagement.aplication.bean.addPatientTreatmentResponse;
import com.ophiux.FeverManagement.aplication.bean.delPatientTreatmentRequest;
import com.ophiux.FeverManagement.aplication.bean.delPatientTreatmentResponse;
import com.ophiux.FeverManagement.aplication.bean.editPatientTreatmentRequest;
import com.ophiux.FeverManagement.aplication.bean.editPatientTreatmentResponse;
import com.ophiux.FeverManagement.aplication.bean.getPatientTreatmentListByDoctorRequest;
import com.ophiux.FeverManagement.aplication.bean.getPatientTreatmentListByDoctorResponse;
import com.ophiux.FeverManagement.aplication.bean.getPatientTreatmentListByPatientRequest;
import com.ophiux.FeverManagement.aplication.bean.getPatientTreatmentListByPatientResponse;
import com.ophiux.FeverManagement.aplication.service.IAddPatientTreatmentAppPort;
import com.ophiux.FeverManagement.aplication.service.IDelPatientTreatmentAppPort;
import com.ophiux.FeverManagement.aplication.service.IEditPatientTreatmentAppPort;
import com.ophiux.FeverManagement.aplication.service.IGetPatientTreatmentListByDoctorAppPort;
import com.ophiux.FeverManagement.aplication.service.IGetPatientTreatmentListByPatientAppPort;
import com.ophiux.micro.core.application.Request;
import com.ophiux.micro.core.application.Result;
import com.ophiux.micro.core.application.ResultEnum;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;

import lombok.extern.slf4j.Slf4j;


@Service
@Slf4j
public class PatientTreatmentService implements IPatientTreatmentPort{
	
	@Autowired
	IAddPatientTreatmentAppPort addPatientTreatmentAppPort;
	@Autowired
	IDelPatientTreatmentAppPort delPatientTreatmentAppPort;
	@Autowired
	IEditPatientTreatmentAppPort editPatientTreatmentAppPort;
	@Autowired
	IGetPatientTreatmentListByDoctorAppPort getPatientTreatmentListByDoctorAppPort;
	@Autowired
	IGetPatientTreatmentListByPatientAppPort getPatientTreatmentListByPatientAppPort;
	
	@Override
	public String addPatientTreatment(String data) {
		log.info("addPatientTreatment接口,请求参数："+data);
		Result<addPatientTreatmentResponse> result = null;
		Request<addPatientTreatmentRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<addPatientTreatmentRequest>>() {
					}.getType();
				}
			});
			result = addPatientTreatmentAppPort.execute(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("addPatientTreatment接口,请求参数："+data+"\n addPatientTreatment接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("addPatientTreatment接口异常",e);
			result = Result.fail(ResultEnum.requestException);
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String editPatientTreatment(String data) {
		log.info("editPatientTreatment接口,请求参数："+data);
		Result<editPatientTreatmentResponse> result = null;
		Request<editPatientTreatmentRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<editPatientTreatmentRequest>>() {
					}.getType();
				}
			});
			result = editPatientTreatmentAppPort.execute(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("editPatientTreatment接口,请求参数："+data+"\n editPatientTreatment接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("editPatientTreatment接口异常",e);
			result = Result.fail(ResultEnum.requestException);
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String delPatientTreatment(String data) {
		log.info("delPatientTreatment接口,请求参数："+data);
		Result<delPatientTreatmentResponse> result = null;
		Request<delPatientTreatmentRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<delPatientTreatmentRequest>>() {
					}.getType();
				}
			});
			result = delPatientTreatmentAppPort.execute(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("delPatientTreatment接口,请求参数："+data+"\n delPatientTreatment接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("delPatientTreatment接口异常",e);
			result = Result.fail(ResultEnum.requestException);
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String getPatientTreatmentListByDoctor(String data) {
		log.info("getPatientTreatmentListByDoctor接口,请求参数："+data);
		Result<List<getPatientTreatmentListByDoctorResponse>> result = null;
		Request<getPatientTreatmentListByDoctorRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<getPatientTreatmentListByDoctorRequest>>() {
					}.getType();
				}
			});
			result = getPatientTreatmentListByDoctorAppPort.execute(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("getPatientTreatmentListByDoctor接口,请求参数："+data+"\n getPatientTreatmentListByDoctor接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("getPatientTreatmentListByDoctor接口异常",e);
			result = Result.fail(ResultEnum.requestException);
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String getPatientTreatmentListByPatient(String data) {
		log.info("getPatientTreatmentListByPatient接口,请求参数："+data);
		Result<List<getPatientTreatmentListByPatientResponse>> result = null;
		Request<getPatientTreatmentListByPatientRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<getPatientTreatmentListByPatientRequest>>() {
					}.getType();
				}
			});
			result = getPatientTreatmentListByPatientAppPort.execute(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("getPatientTreatmentListByPatient接口,请求参数："+data+"\n getPatientTreatmentListByPatient接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("getPatientTreatmentListByPatient接口异常",e);
			result = Result.fail(ResultEnum.requestException);
			return JsonUtils.toJson(result);
		}
	}

}
